package com.hwh.traffic.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapCustomStyleOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.BusLineSearchOption;
import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;
import com.baidu.mapapi.search.core.SearchResult;
import com.hwh.traffic.MapApplication;
import com.hwh.traffic.R;
import com.hwh.traffic.busEntity.Buses;
import com.hwh.traffic.busEntity.NextBuses;
import com.hwh.traffic.utils.BusLineOverlay;
import com.hwh.traffic.utils.DoubleUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



/**
 * @author 黄威海
 * @date 2019年12月10日16:57:22
 * @description 百度地图 Activity
 */
public class MapActivity extends AppCompatActivity{
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LatLng latLng;
    private MapApplication mapApplication;
    private BusLineSearch busLineSearch;
    private BusLineResult mBusLineResult = null; // 保存驾车/步行路线数据的变量，供浏览节点时使用
    private BusLineOverlay mBusLineOverlay; // 公交路线绘制对象
    private String bus_api;
    private Thread t;
    volatile boolean stop = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        initView();
        initMap();

        String neary_stop = getIntent().getStringExtra("BUS_NEXT");
        mBusLineOverlay = new BusLineOverlay(mBaiduMap, neary_stop);
        String bus_line_uid = getIntent().getStringExtra("BUS_LINE_UID");
        System.out.println("MAP BUS UID = " + bus_line_uid);
        bus_api = getIntent().getStringExtra("BUS_API");
        System.out.println("map --------" + bus_line_uid);
        busLineSearch = BusLineSearch.newInstance();

        //确认公交POI UID 的正确方向
        busLineSearch.setOnGetBusLineSearchResultListener(new OnGetBusLineSearchResultListener() {
            @Override
            public void onGetBusLineResult(BusLineResult result) {
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                    return;
                }
                mBaiduMap.clear();
                mBusLineResult = result;
                mBusLineOverlay.removeFromMap();
                mBusLineOverlay.setData(result,latLng);
                mBusLineOverlay.addToMap();
                List<LatLng> points = new ArrayList<LatLng>();
                for (BusLineResult.BusStep step : result.getSteps()) {
                    List<LatLng> wayPoints = step.getWayPoints();
                    points.addAll(wayPoints);
                }
                t = new Thread(() -> {
                    int temp = 1;
                    List<Overlay> list = new ArrayList<>();
                    while (!stop) {
                        System.out.println("线程还在继续未被中断");
                        if (mBaiduMap != null) {
                            NextBuses nextBuses = mBusLineOverlay.updateBusImg(bus_api);
                            if (nextBuses == null){
                                continue;
                            }
                            for (Overlay overlay : list) {
                                overlay.remove();
                            }
                            list.clear();

                            //mBusLineOverlay.addToMap();
                            for (Buses bus : nextBuses.getBuses()) {
                                LatLng latLng = new LatLng(bus.getLat(), bus.getLng());
                                //points.add(latLng);
                                //Collections.sort(points, (o1, o2) -> {
                                //    Double longitude1 = o1.longitude;//X
                                //    Double longitude2 = o2.longitude;
                                //    return longitude1.compareTo(longitude2);
                                //});
                                //int i = points.indexOf(latLng);
                                //LatLng nextLatLng = null;
                                //if (i+1 < points.size()){
                                //    nextLatLng = points.get(i + 1);
                                //}
                                //float degrees = 0.0f;
                                //if (nextLatLng != null){
                                //    double x2 = nextLatLng.latitude;
                                //    double y2 = nextLatLng.longitude;
                                //    String s2 = DoubleUtil.WGS84toECEF(x2, y2);
                                //
                                //    double y1 = latLng.longitude;
                                //    double x1 = latLng.latitude;
                                //    String s1 = DoubleUtil.WGS84toECEF(x1, y1);
                                //    String[] split2 = s2.split("@");
                                //    x2 = Double.parseDouble(split2[0]);
                                //    y2 = Double.parseDouble(split2[1]);
                                //
                                //    String[] split1 = s1.split("@");
                                //    x1 = Double.parseDouble(split1[0]);
                                //    y1 = Double.parseDouble(split1[1]);
                                //
                                //    double dy = DoubleUtil.sub(y2, y1);
                                //    double dx = DoubleUtil.sub(x2, x1);
                                //
                                //    double div = DoubleUtil.div(dy, dx, 2);
                                //    degrees = (float) Math.toDegrees(Math.atan(div));
                                //    System.out.println(degrees);
                                //}

                                //mBaiduMap.clear();
                                BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromAssetWithDpi("behind_bus.png");
                                OverlayOptions option = new MarkerOptions().position(latLng).icon(bitmapDescriptor).anchor(0.5f, 0.5f);
                                Overlay overlay = mBaiduMap.addOverlay(option);
                                list.add(overlay);
                                //points.remove(latLng);

                            }
                            if (nextBuses.getBuses().size() != 0) {
                                Buses bus = nextBuses.getBuses().get(0);
                                LatLng latLng = new LatLng(bus.getLat(), bus.getLng());
                                //points.add(latLng);
                                //Collections.sort(points, (o1, o2) -> {
                                //    Double longitude1 = o1.longitude;//X
                                //    Double longitude2 = o2.longitude;
                                //    return longitude1.compareTo(longitude2);
                                //});
                                //int i = points.indexOf(latLng);
                                //LatLng nextLatLng = null;
                                //if (i+1 < points.size()){
                                //    nextLatLng = points.get(i + 1);
                                //}
                                //float degrees = 0.0f;
                                //if (nextLatLng != null){
                                //    double x2 = nextLatLng.latitude;
                                //    double y2 = nextLatLng.longitude;
                                //    String s2 = DoubleUtil.WGS84toECEF(x2, y2);
                                //
                                //    double y1 = latLng.longitude;
                                //    double x1 = latLng.latitude;
                                //    String s1 = DoubleUtil.WGS84toECEF(x1, y1);
                                //    String[] split2 = s2.split("@");
                                //    x2 = Double.parseDouble(split2[0]);
                                //    y2 = Double.parseDouble(split2[1]);
                                //
                                //    String[] split1 = s1.split("@");
                                //    x1 = Double.parseDouble(split1[0]);
                                //    y1 = Double.parseDouble(split1[1]);
                                //
                                //    double dy = DoubleUtil.sub(y2, y1);
                                //    double dx = DoubleUtil.sub(x2, x1);
                                //
                                //    double div = DoubleUtil.div(dy, dx, 2);
                                //    degrees = (float) Math.toDegrees(Math.atan(div));
                                //    System.out.println(degrees);
                                //}
                                BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromAssetWithDpi("next_bus.png");
                                OverlayOptions option = new MarkerOptions().position(latLng).icon(bitmapDescriptor).zIndex(10).anchor(0.5f, 0.5f);
                                Overlay overlay = mBaiduMap.addOverlay(option);
                                list.add(overlay);
                                //points.remove(latLng);
                            }
                            try {
                                if (temp == 1) {
                                    mBusLineOverlay.zoomToSpan();
                                    temp++;
                                }
                                Thread.sleep(1500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
                mBusLineOverlay.zoomToSpan();
            }
        });
        busLineSearch.searchBusLine((new BusLineSearchOption()
                .city("福州") // 设置查询城市
                .uid(bus_line_uid)));// 设置公交路线uid

    }


    private void initMap() {
        //获取地图控件引用
        mMapView.setMapCustomStyleEnable(true);
        MapCustomStyleOptions mapCustomStyleOptions = new MapCustomStyleOptions();
        mapCustomStyleOptions.customStyleId("729d1b655dea5a678081468b99c1293e");
        mMapView.setMapCustomStyle(mapCustomStyleOptions, new MapView.CustomMapStyleCallBack() {
            @Override
            public boolean onPreLoadLastCustomMapStyle(String s) {
                return false;
            }

            @Override
            public boolean onCustomMapStyleLoadSuccess(boolean b, String s) {
                return false;
            }

            @Override
            public boolean onCustomMapStyleLoadFailed(int i, String s, String s1) {
                return false;
            }
        });
        mMapView.setMapCustomStyleEnable(true);
        mBaiduMap = mMapView.getMap();


        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMyLocationEnabled(true);
        mBaiduMap.setBuildingsEnabled(false);
        mBaiduMap.setCompassEnable(true);


        mapApplication = (MapApplication) getApplication();

        //通过异步执行,防止线程阻塞 因为Application会等待Activity的onCreate执行完后才开始定位
        new Thread() {
            @Override
            public void run() {
                System.out.println("定位成功！！");
                latLng = mapApplication.getLatLng();
                BDLocation bdLocation = mapApplication.getBdLocation();
//                LatLng latLng = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
                System.out.println("获取BDlcation成功" + latLng.toString());
                // 构造定位数据
                MyLocationData locData = new MyLocationData.Builder()
                        .accuracy(bdLocation.getRadius())
                        // 此处设置开发者获取到的方向信息，顺时针0-360
                        .direction(100).latitude(bdLocation.getLatitude())
                        .longitude(bdLocation.getLongitude()).build();
                // 设置定位数据
                mBaiduMap.setMyLocationData(locData);

                //设置地图缩放至定位点
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(latLng).zoom(16.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

            }
        }.start();

    }

    private void initView() {
        mMapView = findViewById(R.id.bmapView);
        ImageView backImg = findViewById(R.id.img_back);
        TextView mText = findViewById(R.id.text_title);
        backImg.setOnClickListener(v -> this.finish());
        mText.setText("实时地图");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
        System.out.println("Destroy");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
        stop = true;
        t.interrupt();
        System.out.println("Pause中断线程");
        //mBaiduMap = null;
    }



}
